package org.geektimes.projects.user.utils;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * @Desc: 数据库工具类
 * @author: liuawei
 * @date: 2021-03-01 17:04
 */
public final class DbUtils {

    public static Connection getNativeConnection() {
        Connection connection = null;
        try {
            DriverManager.setLogWriter(new PrintWriter(System.out));
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
            connection = DriverManager.getConnection("jdbc:derby:/db/user-platform;create=true", new Properties());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static Connection getJNDIConnection() {
        Connection con = null;
        try {
            Context ic = new InitialContext();
            DataSource source = (DataSource)ic.lookup("java:comp/env/jdbc/UserPlatformDB");
            con = source.getConnection();
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

}
